<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    function getAllAdmin($num,$offset)
	{
        $query = $this->db->get('sys_admin',$num,$offset);
        return $query->result();  
    }
	
    function getNumAdmin()
	{
        $query = $this->db->get('sys_admin');
        return $query->num_rows();
    }

    function getAdmin()
	{
        $id = $this->uri->segment(3);
        $this->db->where('admin_id',$id);
        $query = $this->db->get('sys_admin');
        return $query->row();
    }
	
	function get_id($id)
	{
        $this->db->where('admin_id',$id);
        $query = $this->db->get('sys_admin');
        return $query->row();
    }

    function saveAdmin()
	{
        $admin_id = (int)$this->uri->segment(3);
        if($admin_id!=0){
            $data_pass = array(
              'login_name' => $this->input->post('login_name'),
              'password' => md5($this->input->post('password'))
            );
            $data_no_pass = array(
              'login_name' => $this->input->post('login_name')
            );
            if($this->input->post('password')!=''){
                $data = $data_pass;
            }else{
                $data = $data_no_pass;
            }              

            $this->db->where('admin_id',$admin_id);
            if($this->db->update('sys_admin',$data))
			{
                // Get Phan quyen
                $this->db->where('admin_id',$admin_id);
                $this->db->delete('sys_permission_admin');

                $ar_id = $this->input->post('ar_id');
                for($i=0;$i<sizeof($ar_id);$i++)
				{
                    $data_permit = array(
                      'admin_id' => $admin_id,
                      'function_id' => $ar_id[$i]
                    );
                    $this->db->insert('sys_permission_admin',$data_permit);
                }
                return true;
            }else{
                return false;
            }
        }else{
            $data = array(
              'login_name' => $this->input->post('login_name'),
              'password' => md5($this->input->post('password'))
            );              
            if($this->db->insert('sys_admin',$data)){
                $admin_id = $this->db->insert_id();
                $ar_id = $this->input->post('ar_id');

                for($i=0;$i<sizeof($ar_id);$i++){
                    $data_permit = array(
                      'admin_id' => $admin_id,
                      'function_id' => $ar_id[$i]
                    );
                    $this->db->insert('sys_permission_admin',$data_permit);
                }                  
                return true;
            }else{
                return false;
            }
        }
    }

    function delete($id)
    {
          $this->db->where('admin_id',$id);
          $query = $this->db->delete('sys_admin');
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit()
	{
        return $this->db->get('sys_permission')->result();
    }
	
    function get_list_func($permission_id)
	{
        $this->db->where('permission_id',$permission_id);
        return $this->db->get('sys_permission_detail')->result();
    } 

    function get_item_permit_admin($function_id,$admin_id)
	{
        $this->db->where('function_id',$function_id);
        $this->db->where('admin_id',$admin_id);
        $check = $this->db->get('sys_permission_admin')->row();
        if($check){
            return true;
        }else{
            return false;
        }
    }     
}
?>
